package com.TestDemo.Test1;

import java.util.HashSet;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zjz
 * @Date: 2021/10/17/12:33
 * @Description:
 * @Target: 数组，抓到第一个重复的元素
 */
public class catchOneRepeat {

    public static void main(String[] args) {
        int arr[] = new int[]{1,3,2,4,5,10,7,8,9,1};

        int i = UseCatch(arr);
        System.out.println(i);

    }

    // 抓取方法,返回第一个重复的index,-1表示没有
    public static int UseCatch(int arr[]){

        int index = -1;
        if (arr.length==1){
            index =  -1;
        }

        int count = 0; // 计数器
        int i = 0;   // 数组指针
        HashSet<Integer> integers = new HashSet<>();
        while (count==integers.size()&&count<arr.length){
            if(count==arr.length){
                index = -1;
                break;
            }

            integers.add(arr[i++]);
            count++;

        }

        if(count!=0){
            index = count;
        }

        return index;
    }


}
